/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets.register.student;

import AO.Login;
import dbconnector.DBDriver;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "studentimg", urlPatterns = {"/student/registration/DisplayImg"})
public class DisplayImg extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,
            ServletException {
        String userid=((Login)request.getSession().getAttribute("logindata")).getUserid();
        String type=((Login)request.getSession().getAttribute("logindata")).getType();
        //String photoid = request.getParameter("txtid");
        Blob photo = null;
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        String query = "select profilepic from student where userid='"+userid+"'";
        ServletOutputStream out = response.getOutputStream();

   
            try {
               // conn = getMySqlConnection();
            
           
        
            stmt =DBDriver.getConnection("READ");
            rs = stmt.executeQuery(query);
            if (rs.next()) {
                photo = rs.getBlob(1);
                
            }

            response.setContentType("image/jpg");
          //  out.println("hai");

            InputStream in = photo.getBinaryStream();
            int length = (int)photo.length();

            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];

            while ((length = in.read(buffer)) != -1) {
                System.out.println("writing " + length + " bytes");
                out.write(buffer, 0, length);
            }

            in.close();
            out.flush();
        } catch (Exception e) {
            response.setContentType("text/html");
            out.println("<html><head><title>Error: Person Photo</title></head>");
            out.println("<body><h1>Error=" + e.getMessage() + "</h1></body></html>");
            return;
        } finally {
                  }
    }

  
   
}
